/** 
* @author : 褚国强
* @date 创建时间：2021年11月17日 上午8:51:36 
* @version 1.0
*/
package com.bsoft.project.utils;

 import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;

public class Decompressing {
    public static String zipUncompress(String inputFile) throws Exception {
        File srcFile = new File(inputFile);
        // 判断源文件是否存在
        if (!srcFile.exists()) {
            throw new Exception(srcFile.getPath() + "所指文件不存在");
        }
        String destDirPath = inputFile.replace(".zip", "");
        //创建压缩文件对象
        ZipFile zipFile = new ZipFile(srcFile);
        //开始解压
        Enumeration<?> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            // 如果是文件夹，就创建个文件夹
            if (entry.isDirectory()) {
                srcFile.mkdirs();
            } else {
                // 如果是文件，就先创建一个文件，然后用io流把内容copy过去
                File targetFile = new File(destDirPath + "/" + entry.getName());
                // 保证这个文件的父文件夹必须要存在
                if (!targetFile.getParentFile().exists()) {
                    targetFile.getParentFile().mkdirs();
                }
                targetFile.createNewFile();
                // 将压缩文件内容写入到这个文件中
                InputStream is = zipFile.getInputStream(entry);
                FileOutputStream fos = new FileOutputStream(targetFile);
                int len;
                byte[] buf = new byte[1024];
                while ((len = is.read(buf)) != -1) {
                    fos.write(buf, 0, len);
                }
                // 关流顺序，先打开的后关闭
                fos.close();
                is.close();
            }
        }
        zipFile.close();
        return destDirPath;
    }

	public static void readFiles(String inputFile) throws Exception {
		File srcFile = new File(inputFile);
		if (srcFile.isDirectory()) {
			File next[] = srcFile.listFiles();
			for (int i = 0; i < next.length; i++) {
				System.out.println(next[i].getName());
				if (!next[i].isDirectory()) {

					LineIterator lineIterator = FileUtils.lineIterator(next[i], "utf-8");
					long beginTime = System.currentTimeMillis();
					int a = 0;
					while (lineIterator.hasNext()) {
						String result = lineIterator.next();
						String[]starr = result.split("\t");
						if(starr.length != 97){
							System.out.println("----------------- linenum" + a);
						}
						if(a==0){
							for (int j = 0; j < starr.length; j++) {
								System.out.println(starr[j]);
							}
							System.out.println(starr.length);
						}
						if(a%5000 == 0){
							System.out.println("已解析-----------------" + a);
						}
						a++;
					}
					long endTime = System.currentTimeMillis();
					System.out.println(endTime - beginTime);
					System.out.println("总解析-----------------" + a);

					

				}
			}
		}
	}

    public static void main(String[] args) {
        try {
            String path = "D:\\gjyb_down\\H33048300087_1637223928670.zip";
            String zipPath = zipUncompress(path);
            readFiles(zipPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}